//
//  修改密码.m
//  xinhua_sx
//
//  Created by love on 14-8-13.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#define kIOS_VERSION [[UIDevice currentDevice].systemVersion floatValue]
#import "修改密码.h"
#import "AFNetworking.h"
#import "MBProgressHUD.h"

@interface xiugaimimacontroller ()
{
    MBProgressHUD* HUD;
}
@property (nonatomic, strong) UITextField* t1;
@property (nonatomic, strong) UITextField* t2;
@property (nonatomic, strong) UITextField* t3;
@end

@implementation xiugaimimacontroller
@synthesize t1 = _t1;
@synthesize t2 =_t2, t3=_t3;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    CGFloat aY = kIOS_VERSION<7.0?0:20;
    UIView* _navBar = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0+aY)];
    _navBar.backgroundColor = [UIColor colorWithRed:2.0/255 green:102.0/255 blue:172.0/255 alpha:1.0];
    _navBar.userInteractionEnabled = YES;
    
    UILabel* _titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, aY, self.view.frame.size.width, 44)];
    _titleLable.backgroundColor = [UIColor clearColor];
    _titleLable.textColor = [UIColor whiteColor];
    _titleLable.font = [UIFont systemFontOfSize:20];
    _titleLable.textAlignment = NSTextAlignmentCenter;
    _titleLable.text = @"修改密码";
    [_navBar addSubview:_titleLable];
    [self.view addSubview:_navBar];
     UIButton* _leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, aY, 40,40.0)];
    [_leftButton setImage:[UIImage imageNamed:@"icon_back@2x.png"] forState:UIControlStateNormal];
    
    _leftButton.imageEdgeInsets = UIEdgeInsetsMake(10, 13, 5, 5);
    [_leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_leftButton];
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 100, 15)];
    l1.font = [UIFont systemFontOfSize:15];
    l1.backgroundColor = [UIColor clearColor];
    l1.text = @"输入原始密码";
    [self.view addSubview:l1];
    UITextField* t1 = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 280 ,30)];
    t1.font = [UIFont systemFontOfSize:15];
    t1.borderStyle = UITextBorderStyleLine;
    t1.secureTextEntry = YES;
    [self.view addSubview:t1];
    
    UILabel* l2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 100, 15)];
    l2.font = [UIFont systemFontOfSize:15];
    l2.backgroundColor = [UIColor clearColor];
    l2.text = @"输入新密码";
    [self.view addSubview:l2];
    
    UITextField* t2 = [[UITextField alloc] initWithFrame:CGRectMake(20, 170, 280 ,30)];
    t2.font = [UIFont systemFontOfSize:15];
    t2.borderStyle = UITextBorderStyleLine;
    t2.secureTextEntry = YES;
    [self.view addSubview:t2];
    
    UILabel* l3 = [[UILabel alloc] initWithFrame:CGRectMake(20, 220, 100, 15)];
    l3.font = [UIFont systemFontOfSize:15];
    l3.backgroundColor = [UIColor clearColor];
    l3.text = @"确认新密码";
    [self.view addSubview:l3];
    
    UITextField* t3 = [[UITextField alloc] initWithFrame:CGRectMake(20, 240, 280 ,30)];
    t3.font = [UIFont systemFontOfSize:15];
    t3.borderStyle = UITextBorderStyleLine;
    t3.secureTextEntry = YES;
    [self.view addSubview:t3];

    UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(20, 280, 280,30)];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(tijiao) forControlEvents:UIControlEventTouchUpInside];
     [button setBackgroundColor:[xiugaimimacontroller colorWithHexString:@"#A6C4E8"]];
    [self.view addSubview:button];
    self.t1 = t1;
    _t2 = t2;
    _t3 = t3;
    // Do any additional setup after loading the view.
}
-(void)tijiao
{
    if ([_t1.text length] == 0 || [_t2.text length] == 0 || [_t3.text length] == 0 || ![_t3.text isEqualToString:_t2.text])
    {
        UIAlertView* v1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写正确信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [v1 show];
    }
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    HUD.labelText = @"正在提交";
    HUD.dimBackground = YES;
    [self.view addSubview:HUD];
    [HUD show:YES];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSString* uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"UID"];
    if (!uid)
    {
        [HUD hide:YES];
        UIAlertView* v1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请重新登录!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [v1 show];
        
        return;

    }
    NSDictionary* dic = @{@"oldPassword":_t1.text,@"newPassword":_t2.text,@"frontUserId":uid};
    NSString* sting = [NSString stringWithFormat:@"http://183.203.18.45:7001/xinhua/m/imageSave.html"];
    [manager POST:sting parameters:dic success:^(AFHTTPRequestOperation*operation, id responseObject) {
        id jsonObject = [NSJSONSerialization JSONObjectWithData:operation.responseData
                                                        options:NSJSONReadingAllowFragments
                                                          error:nil];
        NSLog(@"json = %@",[[jsonObject objectForKey:@"result"] class]);
        [HUD hide:YES];
        NSString* string = [NSString stringWithFormat:@"%@",jsonObject[@"result"]];
        if ([string isEqualToString:@"success"])
        {
            NSUserDefaults* de = [NSUserDefaults standardUserDefaults];
            [de removeObjectForKey:@"UID"];
            [de synchronize];
            
            NSLog(@"1222");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"Logout1" object:nil];
            NSLog(@"2233");
        }
        //self.usreL.text = [jsonObject objectForKey:@"name"];
        //self.nameL.text = [jsonObject objectForKey:@"nickName"];
        //[self.ve setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://183.203.18.45:7001%@",jsonObject[@"qRcodePath"]]] placeholderImage:[UIImage imageNamed:@"base_detail_default_pic.png"]];
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        [HUD hide:YES];
        UIAlertView* v1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"修改失败!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [v1 show];
        NSLog(@"error = %@",error);
    }];

    
    
}
+ (UIColor *) colorWithHexString: (NSString *)color
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
